#################################################
#################################################
#################################################
#################################################
####### Tuesday 20 December 2011
#################################################
#
# Following imports are temporary for coding module


import generateTable
import expandTable
import queryTable



#################################################
#
# Functions for normalizeTable.py


def makeTable(table):
    newTable = []
    indicators = ['1YrRiskRatio','2YrRiskRatio','3YrRiskRatio','5YrRiskRatio','MedianOSRiskRatio','ResponseRateRiskRatio','Grd3NeutropeniaRiskRatio','Grd3OesToxRiskRatio','Grd3PneumoToxRiskRatio','ToxicDeathRiskRatio']
    k = 1
    for t in table:
    #Note that t here is a dictionary
        for i in indicators:
            if t.get(i,"NA") != "NA": #This checks for an incorrect Key
                newentry = getnewrow(t,i,k)
                newTable.append(newentry)
                k = k+1
    return newTable


def getnewrow(t,i,k):
    new = {}
    new['normID'] = k
    attributes = ['myid','ArmA_TotalShort','ArmB_TotalShort','TrialID','Phase', 'OverallGRADE', 'StageInfo']
    for a in attributes:
        temp = t.get(a,"null")
        if temp != "null":
            new[a] = temp
	if i == '1YrRiskRatio':
		new['Outcome'] = '1YrRiskRatio'
        if t.get('1YrRiskRatioDirection',"NA") != "NA":
            new['Direction'] = t.get('1YrRiskRatioDirection',"NA")
        if t.get('1YrRiskRatio',"NA") != "NA":
            new['Value'] = t.get('1YrRiskRatio',"NA")
        sig = t.get('OS_1_SS',"NA")
        new['Significance'] = getSigValue(sig)
    if i == '2YrRiskRatio':
        new['Outcome'] = '2YrRiskRatio' 
        if t.get('2YrRiskRatioDirection',"NA") != "NA":
            new['Direction'] = t.get('2YrRiskRatioDirection',"NA")
        if t.get('2YrRiskRatio',"NA") != "NA":
            new['Value'] = t.get('2YrRiskRatio',"NA")
        sig = t.get('OS_2_SS',"NA")
        new['Significance'] = getSigValue(sig)
    if i == '3YrRiskRatio':
        new['Outcome'] = '3YrRiskRatio'
        if t.get('3YrRiskRatioDirection',"NA") != "NA":
            new['Direction'] = t.get('3YrRiskRatioDirection',"NA")
        if t.get('3YrRiskRatio',"NA") != "NA":
            new['Value'] = t.get('3YrRiskRatio',"NA")
        sig = t.get('OS_3_SS',"NA")
        new['Significance'] = getSigValue(sig)
    if i == '5YrRiskRatio':
        new['Outcome'] = '5YrRiskRatio'
        if t.get('5YrRiskRatioDirection',"NA") != "NA":
            new['Direction'] = t.get('5YrRiskRatioDirection',"NA")
        if t.get('5YrRiskRatio',"NA") != "NA":
            new['Value'] = t.get('5YrRiskRatio',"NA")
        sig = t.get('OS_5_SS',"NA")
        new['Significance'] = getSigValue(sig)
    if i == 'MedianOSRiskRatio':
        new['Outcome'] = 'MedianOSRiskRatio'
        if t.get('MedianOSRiskRatioDirection',"NA") != "NA":
            new['Direction'] = t.get('MedianOSRiskRatioDirection',"NA")
        if t.get('MedianOSRiskRatio',"NA") != "NA":
            new['Value'] = t.get('MedianOSRiskRatio',"NA")
        sig = t.get('MedOS_SS',"NA")
        new['Significance'] = getSigValue(sig)
    if i == 'ResponseRateRiskRatio':
        new['Outcome'] = 'ResponseRateRiskRatio'
        if t.get('ResponseRateRiskRatioDirection',"NA") != "NA":
            new['Direction'] = t.get('ResponseRateRiskRatioDirection',"NA")
        if t.get('ResponseRateRiskRatio',"NA") != "NA":
            new['Value'] = t.get('ResponseRateRiskRatio',"NA")
        sig = t.get('RR_SS',"NA")
        new['Significance'] = getSigValue(sig)
    if i == 'Grd3NeutropeniaRiskRatio':
        new['Outcome'] = 'Grd3NeutropeniaRiskRatio'
        if t.get('Grd3NeutropeniaRiskRatioDirection',"NA") != "NA":
            new['Direction'] = t.get('Grd3NeutropeniaRiskRatioDirection',"NA")
        if t.get('Grd3NeutropeniaRiskRatio',"NA") != "NA":
            new['Value'] = t.get('Grd3NeutropeniaRiskRatio',"NA")
    if i == 'Grd3OesToxRiskRatio':
        new['Outcome'] = 'Grd3OesToxRiskRatio'
        if t.get('Grd3OesToxRiskRatioDirection',"NA") != "NA":
            new['Direction'] = t.get('Grd3OesToxRiskRatioDirection',"NA")
        if t.get('Grd3OesToxRiskRatio',"NA") != "NA":
            new['Value'] = t.get('Grd3OesToxRiskRatio',"NA")
    if i == 'Grd3PneumoToxRiskRatio':
        new['Outcome'] = 'Grd3PneumoToxRiskRatio'
        if t.get('Grd3PneumoToxRiskRatioDirection',"NA") != "NA":
            new['Direction'] = t.get('Grd3PneumoToxRiskRatioDirection',"NA")
        if t.get('Grd3PneumoToxRiskRatio',"NA") != "NA":
            new['Value'] = t.get('Grd3PneumoToxRiskRatio',"NA")
    if i == 'ToxicDeathRiskRatio':
        new['Outcome'] = 'ToxicDeathRiskRatio'
        if t.get('ToxicDeathRiskRatioDirection',"NA") != "NA":
            new['Direction'] = t.get('ToxicDeathRiskRatioDirection',"NA")
        if t.get('ToxicDeathRiskRatio',"NA") != "NA":
            new['Value'] = t.get('ToxicDeathRiskRatio',"NA")
    return new


def getSigValue(sig):
    outstring ="error"
    s = str(sig)
    if s.startswith("1"):
        outstring = "sig"
    elif sig.startswith("0"):
        outstring = "not_sig"
    else:
        outstring = "null"
    return outstring



#################################################
#
# Following is temporary code for coding module


#myfilename = 'trials.csv'
#print myfilename

#evdb = generateTable.getmydb(myfilename)

#evdb = expandTable.addriskratio(evdb)

#mykeys = evdb[0].keys()

#for c in mykeys:
#    print c

#print queryTable.getmyidlist(evdb)

#normalTable = makeTable(evdb)

#for x in normalTable:
#    print x['Outcome']

    





#################################################
#################################################
#################################################
#################################################

